/* 封装本地存储 */
const TOKEN_KEY = 'hm-news-token'
const CHANNEL_KEY = 'hm-news-channel'
const HISTORY_KEY = 'hm-news-history'

export const getToken = () => {
  // 从localStorage中获取
  return JSON.parse(localStorage.getItem(TOKEN_KEY))
}

export const setToken = token => {
  // localStorage中如果要存储复杂类型的数据，需要先转成JSON字符串进行存储。
  localStorage.setItem(TOKEN_KEY, JSON.stringify(token))
}

export const removeToken = () => {
  localStorage.removeItem(TOKEN_KEY)
}

export const setChannel = channels => {
  localStorage.setItem(CHANNEL_KEY, JSON.stringify(channels))
}

export const getChannel = () => {
  return JSON.parse(localStorage.getItem(CHANNEL_KEY))
}

// 保存历史记录
export const setHistory = history => {
  localStorage.setItem(HISTORY_KEY, JSON.stringify(history))
}

// 获取历史记录
export const getHistory = () => {
  return JSON.parse(localStorage.getItem(HISTORY_KEY))
}
